package cz.muni.fi.pa036.kiss.serviceimpl;

import cz.muni.fi.pa036.kiss.Entity.Student;
import cz.muni.fi.pa036.kiss.Entity.StudentAssembler;
import cz.muni.fi.pa036.kiss.Entity.StudentDTO;
import cz.muni.fi.pa036.kiss.dao.StudentDAO;
import cz.muni.fi.pa036.kiss.service.StudentService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;

public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDAO studentDao;
    @Autowired
    private StudentAssembler assembler;

    @Transactional
    @Override
    public void createStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null: ");
        }
        if (student.getId() != null) {
            throw new IllegalArgumentException("student is not null");
        }
        Student st = assembler.createEntityfromDTO(student);
        studentDao.createStudent(st);
        student.setId(st.getId());
    }

    @Transactional(readOnly = true)
    @Override
    public StudentDTO getStudent(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is null");
        }
        StudentDTO student = assembler.createDTOfromEntity(studentDao.getStudent(id));
        if (student == null) {
            return null;
        } else {
            return student;
        }
    }

    @Transactional
    @Override
    public void removeStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        studentDao.removeStudent(assembler.createEntityfromDTO(student));
    }

    @Transactional
    @Override
    public void updateStudent(StudentDTO student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        studentDao.updateStudent(assembler.createEntityfromDTO(student));
    }

    @Transactional(readOnly = true)
    @Override
    public List<StudentDTO> findAll() {
        List<Student> all = studentDao.findAll();
        List<StudentDTO> allDto = new ArrayList<StudentDTO>();
        for (Student student : all) {
            allDto.add(assembler.createDTOfromEntity(student));
        }
        return allDto;
    }

    @Transactional(readOnly = true)
    @Override
    public List<StudentDTO> findByName(String jmeno, String prijmeni) {
        if (jmeno == null && prijmeni == null) {
            throw new IllegalArgumentException("jmeno & prijmeni is null");
        }
        List<Student> all = studentDao.findByName(jmeno, prijmeni);
        List<StudentDTO> allDto = new ArrayList<StudentDTO>();
        for (Student student : all) {
            allDto.add(assembler.createDTOfromEntity(student));
        }
        return allDto;
    }
}
